package controllers

import (
	"awesomeProject1/modules"
	"awesomeProject1/pkg/app"
	"awesomeProject1/pkg/e"
	"awesomeProject1/services"
	"encoding/json"

	"github.com/gin-gonic/gin"
	uuid "github.com/satori/go.uuid"
)

func GetPortInfo(c *gin.Context) {

	err, data := services.GetPoartInfo()
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
	}

	app.OK(c, gin.H{"value": data}, "查询成功")
}

func UpdatePortInfo(c *gin.Context) {

	a, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(a, &m)

	Ids := m["Id"]
	CompanyNames := m["CompanyName"]
	Moneys := m["Money"]
	FreeDays := m["FreeDay"]

	services.UpdatePortInfo(modules.PortComInfo{
		Id: Ids, CompanyName: CompanyNames, Money: Moneys, FreeDay: FreeDays})

}
func DelPortInfo(c *gin.Context) {

	a, _ := c.GetRawData()
	var m map[string]string
	json.Unmarshal(a, &m)
	services.DelPortInfo(m["ids"])

}

func AddPortInfo(c *gin.Context) {
	a, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(a, &m)

	Ids := uuid.NewV4().String()
	CompanyNames := m["CompanyName"]
	Moneys := m["Money"]
	FreeDays := m["FreeDay"]
	data := modules.PortComInfo{
		Id: Ids, CompanyName: CompanyNames, Money: Moneys, FreeDay: FreeDays}
	err := services.AddPortInfo(data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{"value": data}, "添加成功！")
}
